package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.bean.Category;
import com.itheima.reggie.bean.Dish;
import com.itheima.reggie.bean.Setmeal;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by IntelliJ IDEA.
 * User: 许志民
 * Date: 2021-11-24
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Override
    public boolean delete(Long id) {
        //先查询是否关联相关菜品
        LambdaQueryWrapper<Dish> dishWapper = new LambdaQueryWrapper<>();
        dishWapper.eq(Dish::getCategoryId,id);
        int dishCount = dishService.count(dishWapper);
        if(dishCount > 0){
            throw new CustomException("当前分类有关联菜品，删除对应菜品后方可删除");
        }
        LambdaQueryWrapper<Setmeal> setmealWapper = new LambdaQueryWrapper<>();
        setmealWapper.eq(Setmeal::getCategoryId,id);
        int setmealCount = setmealService.count(setmealWapper);
        if(setmealCount > 0){
            throw new CustomException("当前分类有关联了套餐，删除对应菜品后方可删除");
        }
        return removeById(id);
    }
}
